import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router'
import VuexPersistence from 'vuex-persist'
import { Toast } from 'vant'
const vuexLocal = new VuexPersistence({
    storage: window.localStorage
})
// Vue.use(Vuex);


//创建一个store仓库
const store = new Vuex.Store({
//state用来存放数据的对象
state:{
//初始值、存放的东西
token:'',
wxUserInfo:{},
admin_id:'',
title:'生鲜商城',
base_url:'http://sxbiz.jlxmall.com',
set_address:{},
checkList:{},
cartList:{},
order_detail:{},
goods_id:null,
config:{},
// cart_list:[],
local_cart:[]
// token_info:{}
},
//定义取数据;;;;用来操作修改state的
getters:{
// title:function(state){
//     return state.title;
// }
token:state=>state.wxUserInfo.token?state.wxUserInfo.token.token:'',
wxUserInfo:state=>state.wxUserInfo,
base_url:state=>state.base_url,
admin_id:state=>state.admin_id,
set_address:state=>state.set_address,
checkList:state=>state.checkList,
cartList:state=>state.cartList,
order_detail:state=>state.order_detail,
goods_id:state=>state.goods_id,
config:state=>state.config,
// cart_list:state=>state.cart_list,
local_cart:state=>state.local_cart
},
//存数据
mutations:{
    wxUserInfo(state,data){
        state.wxUserInfo = data
    },
    login_out(state){
        state.wxUserInfo = {}
    },
    user_info(state,data){
        let token = state.wxUserInfo.token;
        let company_address = state.wxUserInfo.company_address;
        state.admin_id = data.admin_id?data.admin_id:state.admin_id;
        state.wxUserInfo = data;
        state.wxUserInfo.token = token;
        state.wxUserInfo.company_address = company_address;
    },
    admin_id(state,data){
        state.admin_id = data
    },
    set_address(state,data){
        state.set_address = data;
    },
    add_cart_list(state,data){
        state.checkList = data.checkList;
        state.cartList = data.cartList;
    },
    address(state,data){
        state.wxUserInfo.company_address = data;
    },
    order_detail(state,data){
        state.order_detail = data;
    },
    updateYue(state,data){
        state.wxUserInfo.integral = data;
    },
    goods_id(state,data){
        state.goods_id = data;
    },
    login_out(state){
        state.wxUserInfo.token = null;
        state.local_cart = [];
    },
    get_config(state,data){
        state.config = data;
    },
    edit_cart(state,data){
        let is_true = false;
        console.log(data.item);
        state.local_cart.forEach(i => {
            if(i.goods_id == data.item.goods_id){
                is_true = true;
                i.num = data.item.num >= 0?data.item.num:data.item.cate_num.num;
            }
        });
        if(!is_true){
            state.local_cart.push({
                cate_id:data.cate_id,
                goods_id:data.item.goods_id,
                num:data.item.num >= 0?data.item.num:data.item.cate_num.num
            })
        }
        Toast('添加成功');
    },
    // add_cart(state,data){
    //     let is_true = false;
    //     state.local_cart.forEach(i => {
    //         if(i.goods_id == data.id){
    //             is_true = true;
    //             i.num++;
    //         }
    //     })
    //     if(!is_true){
    //         state.local_cart.push({
    //             cate_id:data.cate_id,
    //             goods_id:data.id,
    //             num:1
    //         })
    //     }
    // },
    del_cart(state){
        state.local_cart = [];
    }
},
plugins: [vuexLocal.plugin]
})

export default store;